package xyz.weizhi.rjwm.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import xyz.weizhi.rjwm.entity.Setmeal;
import xyz.weizhi.rjwm.entity.vo.R;
import xyz.weizhi.rjwm.entity.vo.SetmealDto;
import xyz.weizhi.rjwm.service.SetmealService;

import java.util.List;
import java.util.Map;

/**
 * @Author milian
 * @Date 2021/9/26 0026 9:46
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    public R<Page> getSetmealPage(@RequestParam("page") long pageNum, long pageSize, String name) {
        Page<Setmeal> page = new Page(pageNum, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        setmealService.page(page, lqw);
        return R.success(page);
    }

    /**
     * @Author milian
     * @Description //新增套餐
     * @Date 15:41 2021/9/26 0026
     * @Param []
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PostMapping
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto){
        boolean b = setmealService.saveWithDish(setmealDto);
        if (b){
            return R.success("添加成功");
        }else {
            return R.error("添加失败");
        }
    }
    /** 
     * @Author milian
     * @Description //查询套餐详情
     * @Date 8:54 2021/9/28 0028
     * @Param [id]
     * @return xyz.weizhi.rjwm.entity.vo.R<xyz.weizhi.rjwm.entity.vo.SetmealDto>
     **/
    @GetMapping("/{id}")
    public R<SetmealDto> querySetmealById(@PathVariable long id){
        log.info("id:{}",id);
        SetmealDto setmealDto = setmealService.queryWithDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> editSetmeal(@RequestBody SetmealDto setmealDto){
        log.info("setmealDto:{}",setmealDto);
        boolean b = setmealService.updateWithDish(setmealDto);
        if (b){
            return R.success("编辑成功");
        }
        return R.error("编辑失败");
    }

    /**
     * @Author milian
     * @Description //变更status状态
     * @Date 9:33 2021/9/28 0028
     * @Param [status, ids]
     * @return xyz.weizhi.rjwm.entity.vo.R<java.lang.String>
     **/
    @PostMapping("/status/{status}")
    public R<String> setmealStatusByStatus(@PathVariable Integer status,long[] ids){
        boolean b = setmealService.updateWithDishByStatus(ids, status);
        if (b){
            return R.success("编辑成功");
        }else {
            return R.error("编辑失败");
        }

    }
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    @DeleteMapping
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        boolean b = setmealService.deleteWithDish(ids);
        if (b){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    @GetMapping("/list")
    public R<List<Setmeal>> getSetmealData(@RequestParam Map map){
        log.info("map:{}",map);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,map.get("categoryId"))
            .eq(Setmeal::getStatus,map.get("status"));
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }

}
